# -*- coding: utf-8 -*-
#
# Author:: JonnyHan
# Date:: 2020/8/18

import os
import re

# 获取project root path
import sys
from pathlib import Path

from compat.common import is_mac, is_windows
from submodules.common.lib import os_

path = os.path.realpath(__file__)
project_path = os.path.split(os.path.split(path)[0])[0]
project_path_parent = str(Path(project_path).parent)
project_name = str(Path(project_path).name)

project_config_path = os.path.join(project_path, "config")
os_.makedirs_softly(project_config_path)

project_tmp_path = os.path.join(project_path_parent, "%s_tmp" % project_name)
os_.makedirs_softly(project_tmp_path)
project_tmp_log_path = os.path.join(project_tmp_path, "log")
os_.makedirs_softly(project_tmp_log_path)
project_tmp_images_path = os.path.join(project_tmp_path, "tmp_images")
os_.makedirs_softly(project_tmp_images_path)
project_tmp_monitor_images_path = os.path.join(project_tmp_path, "monitor_images")
os_.makedirs_softly(project_tmp_monitor_images_path)


def get_local_chrome_version():
    if is_mac():
        cmd_res = os.popen(
            """
            grep -r "Copyright 20[0-9][0-9] Google LLC. Alle Rechte vorbehalten." /Applications/Google\ Chrome.app/Contents/Resources/de.lproj/InfoPlist.strings |grep "CFBundleGetInfoString"
            """
        ).read()
        patten = re.compile("[0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*")
        res = patten.findall(cmd_res)
        chrome_head_version = res[0].split(".")[0]
        return chrome_head_version
    elif is_windows():
        pass


def get_chromedriver_path():
    if is_mac():
        chromedriver_path = os.path.join(
            project_path,
            "drivers",
            "mac",
            "chromedriver",
            get_local_chrome_version(),
            "chromedriver",
        )
    elif is_windows():
        pass
    return chromedriver_path


def get_test_data_path():
    current_path = Path(sys.argv[0])
    project_test_data_path = os.path.join(
        project_path, "test", "data", current_path.with_suffix("").name
    )
    return project_test_data_path


if __name__ == "__main__":
    print(project_path)
